package exttable.testselector;

import exttable.FreezableTableHeader;
import exttable.view.*;
import utils.TestParameters;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Aug 12, 2010
 * Time: 10:43:16 AM
 * To change this template use File | Settings | File Templates.
 */
public class VolunteerSelectorController {
    VolunteerSelectorView view;
    VolunteerSelectorModel model;
    public  static ViewProbationer viewProbationer;


    public VolunteerSelectorController(VolunteerSelectorView view, VolunteerSelectorModel model) {
        this.view = view;
        this.model = model;
        view.addTypeListAction(new TypeListEvent());
        view.addGroupListAction(new GroupListEvent());
        view.addProbationListAction(new ProbationListAction());
        view.addSchemaListAction(new SchemaList());
        view.addPhasesList(new PhasesListEvent());
        new FreezableTableHeader(view.getTimeTable(), view.getTimeTableScroll());
    }

    class TypeListEvent implements ListSelectionListener {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting()) {
                ViewType vt = (ViewType) view.getTypeList().getSelectedValue();
                model.getTestParametrs().setAnalysType(vt.getType());
                try {

                    model.getListPhases().clear();
                    model.getListProbationer().clear();
                    model.getListSchemas().clear();
                    model.getListPhases().addAll(QueryHandler.getPhasesList(vt.getType()));
                    view.getPhasesList().clearSelection();
                    view.getPhasesList().updateUI();
                    view.getSchemaList().updateUI();
                    model.getInfoText()[1] = vt.toString();
                    view.getInfoLabel().setText(model.getInfoText()[0] + " " + model.getInfoText()[1]);
                    clearSelection();
                } catch (Exception rx) {
                    rx.printStackTrace();
                }
            }
        }
    }

    class PhasesListEvent implements ListSelectionListener {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting()) {
                ViewPhases vg = (ViewPhases) view.getPhasesList().getSelectedValue();
                model.getTestParametrs().setPhaseId(vg.getPhaseId());
                try {
                    model.getListProbationer().clear();
                    model.getListSchemas().clear();
                    model.getListProbationer().addAll(QueryHandler.getProbationList(model.getTestParametrs().getAnalysType()
                            , model.getTestParametrs().getPhaseId()));

                    view.getProbationList().updateUI();

                    view.getTypeList().clearSelection();
                    if (view.getSchemaList() != null) {
                        view.getSchemaList().clearSelection();
                    }
                    model.getInfoText()[2] = vg.toString();
                    view.getInfoLabel().setText(model.getInfoText()[0] + " " + model.getInfoText()[1] + " " + model.getInfoText()[2]);
                } catch (Exception exc) {
                    exc.printStackTrace();
                }
            }
        }
    }


    class GroupListEvent implements ListSelectionListener {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting()) {
                ViewGroup vg = (ViewGroup) view.getGroupList().getSelectedValue();
                model.getTestParametrs().setGroupId(vg.getGroupId());
                try {
                    model.getListProbationer().clear();
                    model.getListSchemas().clear();
                    model.getListSchemas().addAll(QueryHandler.getSchemaList(model.getTestParametrs().getGroupId(), model.getTestParametrs().getAnalysType()));
                    view.getSchemaList().updateUI();

                    view.getTypeList().clearSelection();
                    if (view.getSchemaList() != null) {
                        view.getSchemaList().clearSelection();
                    }
                    model.getInfoText()[2] = vg.toString();
                    view.getInfoLabel().setText(model.getInfoText()[0] + " " + model.getInfoText()[1] + " " + model.getInfoText()[2]);
                } catch (Exception exc) {
                    exc.printStackTrace();
                }
            }
        }
    }

    class SchemaList implements ListSelectionListener {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting()) {
                ViewSchema vs = (ViewSchema) view.getSchemaList().getSelectedValue();
                model.getTestParametrs().setSchemaVisitId(vs.getSchemaId());
                try {
                     view.getFrame().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                    updateTimeTable(model.getTestParametrs());
                    view.getFrame().setCursor(Cursor.getDefaultCursor());


                    if (view.getProbationList() != null) {
                        view.getProbationList().clearSelection();
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }
    }


    class ProbationListAction implements ListSelectionListener {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting()) {
                try {
//
                    ViewProbationer vp = (ViewProbationer) view.getProbationList().getSelectedValue();

                    model.getTestParametrs().setProbID(vp.getProbationerId());
                    model.getTestParametrs().setGroupId(vp.getGroupId());
                    model.getTestParametrs().setDateVisitZero(vp.getVizitZero());
                    model.getListSchemas().clear();
                    model.getListSchemas().addAll(QueryHandler.getSchemaList(model.getTestParametrs().getProbID(), model.getTestParametrs().getAnalysType()));
                    view.getSchemaList().updateUI();
//
//                    view.getSchemaList().clearSelection();
                    viewProbationer=vp;
                    model.getInfoText()[3] = vp.toString();
                    view.getInfoLabel().setText(model.getInfoText()[0] + " " + model.getInfoText()[1] + " " + model.getInfoText()[2] + " " + model.getInfoText()[3]);
                    updateTimeTable(model.getTestParametrs());
                } catch (Exception es) {
                    es.printStackTrace();
                }
            }
        }
    }

    private void clearSelection() {
        if (view.getProbationList() != null) {
            view.getProbationList().clearSelection();
        }
        if (view.getSchemaList() != null) {
            view.getSchemaList().clearSelection();
        }
        if (view.getGroupList() != null) {
            view.getGroupList().clearSelection();
        }


    }

    public void updateTimeTable(TestParameters tp) throws Exception {
        model.getTimeTableData().clear();
        model.getTimeTableHeader().clear();
        TimeTableGenerator tg = new TimeTableGenerator(tp.getGroupId(), tp.getAnalysType(), tp.getSchemaVisitId(), tp.getProbID(), tp.getDateVisitZero());
        model.getTimeTableData().addAll(tg.getTimeTable());
        model.getTimeTableHeader().addAll(tg.getVisitNames());
        TimeTableModel ttm = new TimeTableModel(model.getTimeTableData(), model.getTimeTableHeader());

        view.getTimeTable().setModel(ttm);
        for (int i = 1; i < view.getTimeTable().getColumnCount(); i++) {
            view.getTimeTable().getColumnModel().getColumn(i).setCellRenderer(new TimeTableRenderer());
            view.getTimeTable().getColumnModel().getColumn(i).setCellEditor(new TimeTableEditor(new JCheckBox(), tp.getProbID(), tp.getSchemaVisitId()));
            view.getTimeTable().getColumnModel().getColumn(i).setPreferredWidth(98);
        }
        view.getTimeTable().setRowHeight(20);
        view.getTimeTable().getColumnModel().getColumn(0).setPreferredWidth(350);
        view.getTimeTable().setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        view.getTimeTable().updateUI();
        view.getTimeTableScroll().setViewportView(view.getTimeTable());
        view.organiseResultsLayout();
    }
}


//update  schema_visit_time set time_mls = time_mls / 1000 where schema_visits_id <> 9;
